home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Include / object.h < prev    next >
C/C++ Source or Header  |  1998-04-10  |  17KB  |  487 lines

  1. #ifndef Py_OBJECT_H
  2. #define Py_OBJECT_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6.  
  7. /***********************************************************
  8. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  9. The Netherlands.
  10.  
  11.                         All Rights Reserved
  12.  
  13. Permission to use, copy, modify, and distribute this software and its
  14. documentation for any purpose and without fee is hereby granted,
  15. provided that the above copyright notice appear in all copies and that
  16. both that copyright notice and this permission notice appear in
  17. supporting documentation, and that the names of Stichting Mathematisch
  18. Centrum or CWI or Corporation for National Research Initiatives or
  19. CNRI not be used in advertising or publicity pertaining to
  20. distribution of the software without specific, written prior
  21. permission.
  22.  
  23. While CWI is the initial source for this software, a modified version
  24. is made available by the Corporation for National Research Initiatives
  25. (CNRI) at the Internet address ftp://ftp.python.org.
  26.  
  27. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  28. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  29. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  30. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  31. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  32. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  33. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  34. PERFORMANCE OF THIS SOFTWARE.
  35.  
  36. ******************************************************************/
  37.  
  38. /* Object and type object interface */
  39.  
  40. /*
  41. 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
  42.  
  43. Objects are structures allocated on the heap.  Special rules apply to
  44. the use of objects to ensure they are properly garbage-collected.
  45. Objects are never allocated statically or on the stack; they must be
  46. accessed through special macros and functions only.  (Type objects are
  47. exceptions to the first rule; the standard types are represented by
  48. statically initialized type objects.)
  49.  
  50. An object has a 'reference count' that is increased or decreased when a
  51. pointer to the object is copied or deleted; when the reference count
  52. reaches zero there are no references to the object left and it can be
  53. removed from the heap.
  54.  
  55. An object has a 'type' that determines what it represents and what kind
  56. of data it contains.  An object's type is fixed when it is created.
  57. Types themselves are represented as objects; an object contains a
  58. pointer to the corresponding type object.  The type itself has a type
  59. pointer pointing to the object representing the type 'type', which
  60. contains a pointer to itself!).
  61.  
  62. Objects do not float around in memory; once allocated an object keeps
  63. the same size and address.  Objects that must hold variable-size data
  64. can contain pointers to variable-size parts of the object.  Not all
  65. objects of the same type have the same size; but the size cannot change
  66. after allocation.  (These restrictions are made so a reference to an
  67. object can be simply a pointer -- moving an object would require
  68. updating all the pointers, and changing an object's size would require
  69. moving it if there was another object right next to it.)
  70.  
  71. Objects are always accessed through pointers of the type 'PyObject *'.
  72. The type 'PyObject' is a structure that only contains the reference count
  73. and the type pointer.  The actual memory allocated for an object
  74. contains other data that can only be accessed after casting the pointer
  75. to a pointer to a longer structure type.  This longer type must start
  76. with the reference count and type fields; the macro PyObject_HEAD should be
  77. used for this (to accomodate for future changes).  The implementation
  78. of a particular object type can cast the object pointer to the proper
  79. type and back.
  80.  
  81. A standard interface exists for objects that contain an array of items
  82. whose size is determined when the object is allocated.
  83.  
  84. 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
  85. */
  86.  
  87. #ifdef Py_DEBUG
  88.  
  89. /* Turn on heavy reference debugging */
  90. #define Py_TRACE_REFS
  91.  
  92. /* Turn on reference counting */
  93. #define Py_REF_DEBUG
  94.  
  95. #endif /* Py_DEBUG */
  96.  
  97. #ifdef Py_TRACE_REFS
  98. #define PyObject_HEAD \
  99.     struct _object *_ob_next, *_ob_prev; \
  100.     int ob_refcnt; \
  101.     struct _typeobject *ob_type;
  102. #define PyObject_HEAD_INIT(type) 0, 0, 1, type,
  103. #else /* !Py_TRACE_REFS */
  104. #define PyObject_HEAD \
  105.     int ob_refcnt; \
  106.     struct _typeobject *ob_type;
  107. #define PyObject_HEAD_INIT(type) 1, type,
  108. #endif /* !Py_TRACE_REFS */
  109.  
  110. #define PyObject_VAR_HEAD \
  111.     PyObject_HEAD \
  112.     int ob_size; /* Number of items in variable part */
  113.  
  114. typedef struct _object {
  115.     PyObject_HEAD
  116. } PyObject;
  117.  
  118. typedef struct {
  119.     PyObject_VAR_HEAD
  120. } PyVarObject;
  121.  
  122.  
  123. /*
  124. 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
  125.  
  126. Type objects contain a string containing the type name (to help somewhat
  127. in debugging), the allocation parameters (see newobj() and newvarobj()),
  128. and methods for accessing objects of the type.  Methods are optional,a
  129. nil pointer meaning that particular kind of access is not available for
  130. this type.  The Py_DECREF() macro uses the tp_dealloc method without
  131. checking for a nil pointer; it should always be implemented except if
  132. the implementation can guarantee that the reference count will never
  133. reach zero (e.g., for type objects).
  134.  
  135. NB: the methods for certain type groups are now contained in separate
  136. method blocks.
  137. */
  138.  
  139. typedef PyObject * (*unaryfunc) Py_PROTO((PyObject *));
  140. typedef PyObject * (*binaryfunc) Py_PROTO((PyObject *, PyObject *));
  141. typedef PyObject * (*ternaryfunc) Py_PROTO((PyObject *, PyObject *, PyObject *));
  142. typedef int (*inquiry) Py_PROTO((PyObject *));
  143. typedef int (*coercion) Py_PROTO((PyObject **, PyObject **));
  144. typedef PyObject *(*intargfunc) Py_PROTO((PyObject *, int));
  145. typedef PyObject *(*intintargfunc) Py_PROTO((PyObject *, int, int));
  146. typedef int(*intobjargproc) Py_PROTO((PyObject *, int, PyObject *));
  147. typedef int(*intintobjargproc) Py_PROTO((PyObject *, int, int, PyObject *));
  148. typedef int(*objobjargproc) Py_PROTO((PyObject *, PyObject *, PyObject *));
  149. typedef int (*getreadbufferproc) Py_PROTO((PyObject *, int, void **));
  150. typedef int (*getwritebufferproc) Py_PROTO((PyObject *, int, void **));
  151. typedef int (*getsegcountproc) Py_PROTO((PyObject *, int *));
  152.  
  153. typedef struct {
  154.     binaryfunc nb_add;
  155.     binaryfunc nb_subtract;
  156.     binaryfunc nb_multiply;
  157.     binaryfunc nb_divide;
  158.     binaryfunc nb_remainder;
  159.     binaryfunc nb_divmod;
  160.     ternaryfunc nb_power;
  161.     unaryfunc nb_negative;
  162.     unaryfunc nb_positive;
  163.     unaryfunc nb_absolute;
  164.     inquiry nb_nonzero;
  165.     unaryfunc nb_invert;
  166.     binaryfunc nb_lshift;
  167.     binaryfunc nb_rshift;
  168.     binaryfunc nb_and;
  169.     binaryfunc nb_xor;
  170.     binaryfunc nb_or;
  171.     coercion nb_coerce;
  172.     unaryfunc nb_int;
  173.     unaryfunc nb_long;
  174.     unaryfunc nb_float;
  175.     unaryfunc nb_oct;
  176.     unaryfunc nb_hex;
  177. } PyNumberMethods;
  178.  
  179. typedef struct {
  180.     inquiry sq_length;
  181.     binaryfunc sq_concat;
  182.     intargfunc sq_repeat;
  183.     intargfunc sq_item;
  184.     intintargfunc sq_slice;
  185.     intobjargproc sq_ass_item;
  186.     intintobjargproc sq_ass_slice;
  187. } PySequenceMethods;
  188.  
  189. typedef struct {
  190.     inquiry mp_length;
  191.     binaryfunc mp_subscript;
  192.     objobjargproc mp_ass_subscript;
  193. } PyMappingMethods;
  194.  
  195. typedef struct {
  196.     getreadbufferproc bf_getreadbuffer;
  197.     getwritebufferproc bf_getwritebuffer;
  198.     getsegcountproc bf_getsegcount;
  199. } PyBufferProcs;
  200.     
  201.  
  202. typedef void (*destructor) Py_PROTO((PyObject *));
  203. typedef int (*printfunc) Py_PROTO((PyObject *, FILE *, int));
  204. typedef PyObject *(*getattrfunc) Py_PROTO((PyObject *, char *));
  205. typedef PyObject *(*getattrofunc) Py_PROTO((PyObject *, PyObject *));
  206. typedef int (*setattrfunc) Py_PROTO((PyObject *, char *, PyObject *));
  207. typedef int (*setattrofunc) Py_PROTO((PyObject *, PyObject *, PyObject *));
  208. typedef int (*cmpfunc) Py_PROTO((PyObject *, PyObject *));
  209. typedef PyObject *(*reprfunc) Py_PROTO((PyObject *));
  210. typedef long (*hashfunc) Py_PROTO((PyObject *));
  211.  
  212. typedef struct _typeobject {
  213.     PyObject_VAR_HEAD
  214.     char *tp_name; /* For printing */
  215.     int tp_basicsize, tp_itemsize; /* For allocation */
  216.     
  217.     /* Methods to implement standard operations */
  218.     
  219.     destructor tp_dealloc;
  220.     printfunc tp_print;
  221.     getattrfunc tp_getattr;
  222.     setattrfunc tp_setattr;
  223.     cmpfunc tp_compare;
  224.     reprfunc tp_repr;
  225.     
  226.     /* Method suites for standard classes */
  227.     
  228.     PyNumberMethods *tp_as_number;
  229.     PySequenceMethods *tp_as_sequence;
  230.     PyMappingMethods *tp_as_mapping;
  231.  
  232.     /* More standard operations (at end for binary compatibility) */
  233.  
  234.     hashfunc tp_hash;
  235.     ternaryfunc tp_call;
  236.     reprfunc tp_str;
  237.     getattrofunc tp_getattro;
  238.     setattrofunc tp_setattro;
  239.  
  240.     /* Functions to access object as input/output buffer */
  241.     PyBufferProcs *tp_as_buffer;
  242.     
  243.     /* Space for future expansion */
  244.     long tp_xxx4;
  245.  
  246.     char *tp_doc; /* Documentation string */
  247.  
  248. #ifdef COUNT_ALLOCS
  249.     /* these must be last */
  250.     int tp_alloc;
  251.     int tp_free;
  252.     int tp_maxalloc;
  253.     struct _typeobject *tp_next;
  254. #endif
  255. } PyTypeObject;
  256.  
  257. extern DL_IMPORT(PyTypeObject) PyType_Type; /* The type of type objects */
  258.  
  259. #define PyType_Check(op) ((op)->ob_type == &PyType_Type)
  260.  
  261. /* Generic operations on objects */
  262. extern int PyObject_Print Py_PROTO((PyObject *, FILE *, int));
  263. extern PyObject * PyObject_Repr Py_PROTO((PyObject *));
  264. extern PyObject * PyObject_Str Py_PROTO((PyObject *));
  265. extern int PyObject_Compare Py_PROTO((PyObject *, PyObject *));
  266. extern PyObject *PyObject_GetAttrString Py_PROTO((PyObject *, char *));
  267. extern int PyObject_SetAttrString Py_PROTO((PyObject *, char *, PyObject *));
  268. extern int PyObject_HasAttrString Py_PROTO((PyObject *, char *));
  269. extern PyObject *PyObject_GetAttr Py_PROTO((PyObject *, PyObject *));
  270. extern int PyObject_SetAttr Py_PROTO((PyObject *, PyObject *, PyObject *));
  271. extern int PyObject_HasAttr Py_PROTO((PyObject *, PyObject *));
  272. extern long PyObject_Hash Py_PROTO((PyObject *));
  273. extern int PyObject_IsTrue Py_PROTO((PyObject *));
  274. extern int PyObject_Not Py_PROTO((PyObject *));
  275. extern int PyCallable_Check Py_PROTO((PyObject *));
  276. extern int PyNumber_Coerce Py_PROTO((PyObject **, PyObject **));
  277. extern int PyNumber_CoerceEx Py_PROTO((PyObject **, PyObject **));
  278.  
  279. /* Helpers for printing recursive container types */
  280. extern int Py_ReprEnter Py_PROTO((PyObject *));
  281. extern void Py_ReprLeave Py_PROTO((PyObject *));
  282.  
  283. /* Flag bits for printing: */
  284. #define Py_PRINT_RAW    1    /* No string quotes etc. */
  285.  
  286. /*
  287. 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
  288.  
  289. The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
  290. reference counts.  Py_DECREF calls the object's deallocator function; for
  291. objects that don't contain references to other objects or heap memory
  292. this can be the standard function free().  Both macros can be used
  293. whereever a void expression is allowed.  The argument shouldn't be a
  294. NIL pointer.  The macro _Py_NewReference(op) is used only to initialize
  295. reference counts to 1; it is defined here for convenience.
  296.  
  297. We assume that the reference count field can never overflow; this can
  298. be proven when the size of the field is the same as the pointer size
  299. but even with a 16-bit reference count field it is pretty unlikely so
  300. we ignore the possibility.  (If you are paranoid, make it a long.)
  301.  
  302. Type objects should never be deallocated; the type pointer in an object
  303. is not considered to be a reference to the type object, to save
  304. complications in the deallocation function.  (This is actually a
  305. decision that's up to the implementer of each new type so if you want,
  306. you can count such references to the type object.)
  307.  
  308. *** WARNING*** The Py_DECREF macro must have a side-effect-free argument
  309. since it may evaluate its argument multiple times.  (The alternative
  310. would be to mace it a proper function or assign it to a global temporary
  311. variable first, both of which are slower; and in a multi-threaded
  312. environment the global variable trick is not safe.)
  313. */
  314.  
  315. #ifdef Py_TRACE_REFS
  316. #ifndef Py_REF_DEBUG
  317. #define Py_REF_DEBUG
  318. #endif
  319. #endif
  320.  
  321. #ifdef Py_TRACE_REFS
  322. extern void _Py_Dealloc Py_PROTO((PyObject *));
  323. extern void _Py_NewReference Py_PROTO((PyObject *));
  324. extern void _Py_ForgetReference Py_PROTO((PyObject *));
  325. extern void _Py_PrintReferences Py_PROTO((FILE *));
  326. #endif
  327.  
  328. #ifndef Py_TRACE_REFS
  329. #ifdef COUNT_ALLOCS
  330. #define _Py_Dealloc(op) ((op)->ob_type->tp_free++, (*(op)->ob_type->tp_dealloc)((PyObject *)(op)))
  331. #define _Py_ForgetReference(op) ((op)->ob_type->tp_free++)
  332. #else /* !COUNT_ALLOCS */
  333. #define _Py_Dealloc(op) (*(op)->ob_type->tp_dealloc)((PyObject *)(op))
  334. #define _Py_ForgetReference(op) /*empty*/
  335. #endif /* !COUNT_ALLOCS */
  336. #endif /* !Py_TRACE_REFS */
  337.  
  338. #ifdef COUNT_ALLOCS
  339. extern void inc_count Py_PROTO((PyTypeObject *));
  340. #endif
  341.  
  342. #ifdef Py_REF_DEBUG
  343.  
  344. extern long _Py_RefTotal;
  345.  
  346. #ifndef Py_TRACE_REFS
  347. #ifdef COUNT_ALLOCS
  348. #define _Py_NewReference(op) (inc_count((op)->ob_type), _Py_RefTotal++, (op)->ob_refcnt = 1)
  349. #else
  350. #define _Py_NewReference(op) (_Py_RefTotal++, (op)->ob_refcnt = 1)
  351. #endif
  352. #endif /* !Py_TRACE_REFS */
  353.  
  354. #define Py_INCREF(op) (_Py_RefTotal++, (op)->ob_refcnt++)
  355. #define Py_DECREF(op) \
  356.     if (--_Py_RefTotal, --(op)->ob_refcnt != 0) \
  357.         ; \
  358.     else \
  359.         _Py_Dealloc((PyObject *)(op))
  360. #else /* !Py_REF_DEBUG */
  361.  
  362. #ifdef COUNT_ALLOCS
  363. #define _Py_NewReference(op) (inc_count((op)->ob_type), (op)->ob_refcnt = 1)
  364. #else
  365. #define _Py_NewReference(op) ((op)->ob_refcnt = 1)
  366. #endif
  367.  
  368. #define Py_INCREF(op) ((op)->ob_refcnt++)
  369. #define Py_DECREF(op) \
  370.     if (--(op)->ob_refcnt != 0) \
  371.         ; \
  372.     else \
  373.         _Py_Dealloc((PyObject *)(op))
  374. #endif /* !Py_REF_DEBUG */
  375.  
  376. /* Macros to use in case the object pointer may be NULL: */
  377.  
  378. #define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op)
  379. #define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)
  380.  
  381. /* Definition of NULL, so you don't have to include <stdio.h> */
  382.  
  383. #ifndef NULL
  384. #define NULL 0
  385. #endif
  386.  
  387.  
  388. /*
  389. _Py_NoneStruct is an object of undefined type which can be used in contexts
  390. where NULL (nil) is not suitable (since NULL often means 'error').
  391.  
  392. Don't forget to apply Py_INCREF() when returning this value!!!
  393. */
  394.  
  395. extern DL_IMPORT(PyObject) _Py_NoneStruct; /* Don't use this directly */
  396.  
  397. #define Py_None (&_Py_NoneStruct)
  398.  
  399.  
  400. /*
  401. A common programming style in Python requires the forward declaration
  402. of static, initialized structures, e.g. for a type object that is used
  403. by the functions whose address must be used in the initializer.
  404. Some compilers (notably SCO ODT 3.0, I seem to remember early AIX as
  405. well) botch this if you use the static keyword for both declarations
  406. (they allocate two objects, and use the first, uninitialized one until
  407. the second declaration is encountered).  Therefore, the forward
  408. declaration should use the 'forwardstatic' keyword.  This expands to
  409. static on most systems, but to extern on a few.  The actual storage
  410. and name will still be static because the second declaration is
  411. static, so no linker visible symbols will be generated.  (Standard C
  412. compilers take offense to the extern forward declaration of a static
  413. object, so I can't just put extern in all cases. :-( )
  414. */
  415.  
  416. #ifdef BAD_STATIC_FORWARD
  417. #define staticforward extern
  418. #ifdef __SC__
  419. #define statichere
  420. #else
  421. #define statichere static
  422. #endif /* __SC__ */
  423. #else /* !BAD_STATIC_FORWARD */
  424. #define staticforward static
  425. #define statichere static
  426. #endif /* !BAD_STATIC_FORWARD */
  427.  
  428.  
  429. /*
  430. 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
  431.  
  432. More conventions
  433. ================
  434.  
  435. Argument Checking
  436. -----------------
  437.  
  438. Functions that take objects as arguments normally don't check for nil
  439. arguments, but they do check the type of the argument, and return an
  440. error if the function doesn't apply to the type.
  441.  
  442. Failure Modes
  443. -------------
  444.  
  445. Functions may fail for a variety of reasons, including running out of
  446. memory.  This is communicated to the caller in two ways: an error string
  447. is set (see errors.h), and the function result differs: functions that
  448. normally return a pointer return NULL for failure, functions returning
  449. an integer return -1 (which could be a legal return value too!), and
  450. other functions return 0 for success and -1 for failure.
  451. Callers should always check for errors before using the result.
  452.  
  453. Reference Counts
  454. ----------------
  455.  
  456. It takes a while to get used to the proper usage of reference counts.
  457.  
  458. Functions that create an object set the reference count to 1; such new
  459. objects must be stored somewhere or destroyed again with Py_DECREF().
  460. Functions that 'store' objects such as PyTuple_SetItem() and
  461. PyDict_SetItemString()
  462. don't increment the reference count of the object, since the most
  463. frequent use is to store a fresh object.  Functions that 'retrieve'
  464. objects such as PyTuple_GetItem() and PyDict_GetItemString() also
  465. don't increment
  466. the reference count, since most frequently the object is only looked at
  467. quickly.  Thus, to retrieve an object and store it again, the caller
  468. must call Py_INCREF() explicitly.
  469.  
  470. NOTE: functions that 'consume' a reference count like
  471. PyList_SetItemString() even consume the reference if the object wasn't
  472. stored, to simplify error handling.
  473.  
  474. It seems attractive to make other functions that take an object as
  475. argument consume a reference count; however this may quickly get
  476. confusing (even the current practice is already confusing).  Consider
  477. it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
  478. times.
  479.  
  480. 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
  481. */
  482.  
  483. #ifdef __cplusplus
  484. }
  485. #endif
  486. #endif /* !Py_OBJECT_H */
  487.